// proxySubShopPartner.js
const app = getApp()
const util = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopid:'',
    agent:{},
    agentShow:false,
    serive:{},
    seriveShow: false,
    saleman:{},
    salemanShow: false,

    shopWayshow:false,
    actionShow: false,
    type:'shop',
    state:"add",
    name:'',
    id: '',
    idError:'',
    telphone:'',
    commission:'',
    disabled:false,

    memberShow:false,
    member:[],
    shopcommission:'',

    staffRateDialog: false,
    staffRate: '',

    surplusRate: '',
  },
  // 删除人员
  Todelete: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    wx.showModal({
      title: '提示',
      content: '请确认是否删除',
      success:function(res){
        if(res.confirm){
          var data = {
            type: type,
            id: that.data.shopid,
            uid: '',
            name: '',
            telphone: '',
            commission: ''
          };
          that.manageAPI(JSON.stringify(data))
        }
      }
    })
  },
  manageAPI:function(data){
    var that = this;
    var data = JSON.parse(data);
    wx.showLoading({
      title: '加载中...',
    })
    app.post('agent/submitShopStaff', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        that.actionClose();
      } else {
        wx.showToast({
          title: res.message,
          icon: 'loading',
          duration: 2000
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      shopid:options.id,
      shopcommission: options.commission,
      agentRate: app.globalData.agentRate
    })
    that.list();
  },
  // 没有数据
  nodata:function(){
    var that = this;
    if (that.data.agent !== null && JSON.stringify(that.data.agent) !== '{}') {
      that.setData({
        agentShow: true
      })
    }else{
      that.setData({
        agentShow: false
      })
    }
    if (that.data.serive !== null && JSON.stringify(that.data.serive) !== '{}') {
      that.setData({
        seriveShow: true
      })
    }else{
      that.setData({
        seriveShow: false
      })
    }
    if (that.data.saleman !== null && JSON.stringify(that.data.saleman) !== '{}') {
      that.setData({
        salemanShow: true
      })
    }else{
      that.setData({
        salemanShow: false
      })
    }
  },
  // 商家及工作人员信息
  list: function () {
    var that = this;
    var data = { id: that.data.shopid };
    app.post('agent/shopStaff', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        that.setData({
          agent: res.data.shop,
          serive: res.data.engineer,
          saleman: res.data.salesman,
        })
        that.nodata();
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: "网络错误",
        icon: 'loading',
        duration: 2000
      })
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.list();
    wx.stopPullDownRefresh()
  }
})